<?php
/***************************************************************************
*                                register.php
 *
 *   Begin date           	: 
 *   Copyright            	: 
 *   Email                	: 
 ***************************************************************************/
defined('PHP5_PHP') or die("Application is stopping!!!");
if(checkLoginUser()) {
	php5Redirect(sefBuild($php5WebPath, '', 1));
	return;
}
require_once($php5RootPath . "/classes/user.class.php");

$row = new php5User($php5DB);

$task 		= php5GetParam($_REQUEST, 'task', '');

$smarty->setCompileDir(sprintf($php5RootTempalteCPath, 'users'));
$tpl = sprintf($php5TemplateFile, $language, 'register', 'main.tpl');
if(!is_file($tpl )) {
	die("can not find template");
}
//check task
$msg = '';
$noerr = 1;
//type
$arrTemp = $define->USER_TYPE();
$types = array();
$types[0] = "---";
foreach($arrTemp as $k => $v) {
	if($k < 2) continue;
	$types[$k] = $v;
}

switch($task)
{
	case 'update':
		$row->bind( $_POST );
		if(!trim($row->username)) {
			$noerr = 0;
			$msgAlert = sprintf($lang['_BLANK_MSG_'], 'User ID');
			break;
		}
		if( preg_match("/^([a-zA-Z0-9])+$/", $row->username )==false) {
			$noerr = 0;
			$msg = $msgAlert = sprintf($lang['_INVALID_MSG_'], 'Distributor ID');
			break;
		}
		$where = array();
		$where[] = "username = '".mysql_real_escape_string($row->username)."'";
		$query = "SELECT COUNT(*) "
		. "\n FROM #__users "
		. ( count( $where ) ? "\n WHERE " . implode( ' AND ', $where ) : "" )
		;
		
		$php5DB->setQuery( $query );
		$count = $php5DB->loadResult();	
		if( $count) {
			$noerr = 0;
			$msgAlert = $lang['_MSS_015_'];
			break;
		}		
		if(!trim($row->name)) {
			$noerr = 0;
			$msg = $msgAlert = sprintf($lang['_BLANK_MSG_'], 'Name');
			break;
		}
		
		if(!trim($row->email)) {
			$noerr = 0;
			$msg = $msgAlert = sprintf($lang['_BLANK_MSG_'], 'Email');
			break;
		}
		
		if( preg_match("/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/", $row->email )==false) {
			$noerr = 0;
			$msg = $msgAlert = sprintf($lang['_INVALID_MSG_'], 'Email');
			break;
		}
		$where = array();
		$where[] = "email = '".mysql_real_escape_string($row->email)."'";
		$query = "SELECT COUNT(*) "
		. "\n FROM #__users "
		. ( count( $where ) ? "\n WHERE " . implode( ' AND ', $where ) : "" )
		;
		
		$php5DB->setQuery( $query );
		$count = $php5DB->loadResult();	
		if( $count) {
			$noerr = 0;
			$msg = $msgAlert = $lang['_MSS_003_'];
			break;
		}
					
		$newpassowrd 		= trim(php5GetParam($_REQUEST, 'newpassowrd', ''));
		$retypepassowrd 		= trim(php5GetParam($_REQUEST, 'retypepassowrd', ''));

		//$row->password = md5($newpassowrd);
		$rand = rand(1000, 999999);
		$row->password = md5($rand);
		$row->show_pass = $rand;
		$row->store();
		
		if($noerr) {
			$query = "REPLACE INTO #__active_user"
        . "\n SET id='%s', user_id='%d'"
        ;
			$key = md5(time());
			$php5DB->setQuery( sprintf($query, $key, $row->id) );
			$php5DB->query();
			$link = $php5WebPath . '/active.php?task=token&id=' . $key;
			//$row->country_id = '196';
			if (!$row->store()) {
				$msg = $msgAlert = "Error!!! Please try again!!!";		
			} else {
				$body = nl2br(sprintf($lang['_MSS_002_'], $row->name, $row->username, $row->show_pass, $link));
				
				send_email_all($row->email, $lang['_MSS_001_'], $body);
				
				//php5Mail( php5GetConfig('config_email'), php5GetConfig('config_sender'), $row->email, $lang['_MSS_001_'], $body, 1,'', array(php5GetConfig('config_admin_email')));
				$msg = $msgAlert = "Account created successfully. Please check your email to active";
				$redirect = sefBuild($php5WebPath, 'index.php?o=users&m=main', 1);
			}
			
		}
		break;
	default:
		break;	
		
}
if($msg) {
	$smarty->assign('msg', $msg);
}
//assign
$smarty->assign('username', $row->username);
$smarty->assign('name', $row->name);
$smarty->assign('email', $row->email);
$smarty->assign('phone', $row->phone);
$smarty->assign('type_id', $row->type_id);
$smarty->assign('types', $types);
$smarty->assign('birthday', $row->birthday);
$smarty->assign('address', $row->address);
$smarty->assign('city', $row->city);
$smarty->assign('country', $row->country);
$arr_temp = $define->COUNTRY();
$countries[0] = "-- Select country --";
foreach($arr_temp as $key => $arr) {	
	$countries[$key] = $arr['name'];
}
$smarty->assign('countries', $countries);
$smarty->assign('link_return', $php5WebPath);
$smarty->assign('action', sefBuild($php5WebPath, 'index.php?o=register&m=main&task=update', 1));
$smarty->assign('main', $smarty->fetch($tpl));

return;
